package org.hgvc.webapp.ets.jsf.beans.voucher;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.faces.application.Application;
import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Guest;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.bo.Prospect;
import org.hgvc.webapp.ets.bo.Voucher;
import org.hgvc.webapp.ets.bo.VoucherEntry;
import org.hgvc.webapp.ets.bo.VoucherEntryDetail;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.jsf.beans.AckBean;
import org.hgvc.webapp.ets.jsf.beans.ReportBean;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.hgvc.webapp.ets.spring.service.VoucherService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class VoucherMgmtBean extends AbstractBean{
	public static final String BEAN_NAME="voucherMgmtBean";
	
	private VoucherService voucherService;
	private PartnerService partnerService;
	private MemberService memberService;
	//---Voucher Generation----------
	private Voucher voucher;
	private Integer voucherQty;
	private String returnAckMsg;
	private List<SelectItem> selectProductList = new ArrayList<SelectItem>();
	//-------------------------------
	//---Voucher Assignment----------------------
	private List<SelectItem> selectAssignTypeList = new ArrayList<SelectItem>();
	private List<SelectItem> selectAssignToList = new ArrayList<SelectItem>();
	private List<Agent> agentList=new ArrayList<Agent>();
	private List<User> userList=new ArrayList<User>();
	private List<Voucher> voucherList=new ArrayList<Voucher>();
	private String assignType;
	private String assignToId;
	private Voucher startVoucher;
	private Voucher lastVoucher;
	private Agent agent;
	private User hgvcUser;
	private List<SelectItem> selectAgentList = new ArrayList<SelectItem>();
	private List<SelectItem> selectVoucherUnassignList = new ArrayList<SelectItem>();
	private String assignTo;
	//--------------------------------------------
	//----Voucher Listing Report--------------------
	private String voucherNo;
	private Date effectiveDate;
	private Date expiryDate;
	//--------------------------------------------
	private String ackTitle;
	private String ackBindingMethod;
	private HtmlCommandButton htmlCmdButton;
	//---------Voucher Entry------------------------
	private List<SelectItem> selectMemberList = new ArrayList<SelectItem>();
	private List<SelectItem> selectAgreementList = new ArrayList<SelectItem>();
	private List<SelectItem> selectVoucherEntryTypeList = new ArrayList<SelectItem>();
	private List<SelectItem> selectVoucherList = new ArrayList<SelectItem>();
	private List<SelectItem> selectProspectList = new ArrayList<SelectItem>();
	private List<Member> memberList;
	private List<VoucherEntryDetail> voucherEntryDetailList;
	private Member member;
	private Agreement agreement;
	private Guest guest;
	private Prospect prospect;
	private VoucherEntry voucherEntry;
	private VoucherEntryDetail voucherEntryDetail;
	private String voucherEntryType;
	
	//----------------------------------------------
	private boolean catchError;
	private boolean lookupForm;
	private boolean startVoucherLookup;
	private boolean lastVoucherLookup;
	private List<Voucher> voucherAssignedList=new ArrayList<Voucher>();
	
	public void setVoucherService(VoucherService voucherService) {
		this.voucherService = voucherService;
	}
	public void setPartnerService(PartnerService partnerService) {
		this.partnerService = partnerService;
	}
	
	//--------Voucher Generation-----------------------------
	public Voucher getVoucher() {
		return voucher;
	}
	public void setVoucher(Voucher voucher) {
		this.voucher = voucher;
	}
	public Integer getVoucherQty() {
		return voucherQty;
	}
	public void setVoucherQty(Integer voucherQty) {
		this.voucherQty = voucherQty;
	}
	public List<SelectItem> getSelectProductList() {
		return selectProductList;
	}
	public void setSelectProductList(List<SelectItem> selectProductList) {
		this.selectProductList = selectProductList;
	}
	public String getReturnAckMsg() {
		return returnAckMsg;
	}
	public String initVoucherGenerationAction(){
		this.lookupForm=false;
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		voucher=new Voucher();
		voucher.setProduct1(new PartnerProduct());
		voucher.setProduct2(new PartnerProduct());
		voucher.setProduct3(new PartnerProduct());
		voucher.setProduct4(new PartnerProduct());
		voucher.setProduct5(new PartnerProduct());
		this.returnAckMsg="";
		selectProductList = new ArrayList<SelectItem>();
		try{
			List<PartnerProduct> partnerProductList=this.partnerService.getPartnerProductApprovedList();
			for (PartnerProduct partnerProduct : partnerProductList){
				SelectItem selectItem = new SelectItem(partnerProduct.getPartnerProductId(),partnerProduct.getCode()+" - "+partnerProduct.getDesc());
				selectProductList.add(selectItem);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public String generateVoucherAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void generateVoucherActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.returnAckMsg =voucherService.generateVoucher(this.voucher, this.voucherQty);
			
			AckBean ack = (AckBean)getNewManagedBean(AckBean.BEAN_NAME);
			ack.setAckHeaderTitle(SpringUtils.getMessage("voucher.generation.label", new Object[]{}));
			ack.setAckMsg(this.returnAckMsg);
			ack.setNextActionOutcome("launchVoucherGenPage_"+getRoleName());
			
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	//---------------------Voucher Generation END-----------------------------------------------
	//---------------------Voucher Assignment---------------------------------------------------
	public Voucher getStartVoucher() {
		return startVoucher;
	}
	public void setStartVoucher(Voucher startVoucher) {
		this.startVoucher = startVoucher;
	}
	public Voucher getLastVoucher() {
		return lastVoucher;
	}
	public void setLastVoucher(Voucher lastVoucher) {
		this.lastVoucher = lastVoucher;
	}
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public User getHgvcUser() {
		return hgvcUser;
	}
	public void setHgvcUser(User hgvcUser) {
		this.hgvcUser = hgvcUser;
	}
	public String getAssignType() {
		return assignType;
	}
	public void setAssignType(String assignType) {
		this.assignType = assignType;
	}
	public List<SelectItem> getSelectAssignTypeList() {
		return selectAssignTypeList;
	}
	public void setSelectAssignTypeList(List<SelectItem> selectAssignTypeList) {
		this.selectAssignTypeList = selectAssignTypeList;
	}
	public List<SelectItem> getSelectAgentList() {
		return selectAgentList;
	}
	public void setSelectAgentList(List<SelectItem> selectAgentList) {
		this.selectAgentList = selectAgentList;
	}
	public List<SelectItem> getSelectVoucherUnassignList() {
		return selectVoucherUnassignList;
	}
	public void setSelectVoucherUnassignList(
			List<SelectItem> selectVoucherUnassignList) {
		this.selectVoucherUnassignList = selectVoucherUnassignList;
	}
	
	public String getAssignToLabel(){
		String label="";
		if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT))
			label= SpringUtils.getMessage("voucher.agent.label",new Object[]{});
		else
			label= SpringUtils.getMessage("voucher.employee.label",new Object[]{});
		return label;
	}
	public List<SelectItem> getSelectAssignToList() {
		this.selectAssignToList = new ArrayList<SelectItem>();
		if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT)){
			for (Agent agent : agentList){
				SelectItem selectItem = new SelectItem(agent.getAgentId(),agent.getCode()+" - "+agent.getCompName());
				selectAssignToList.add(selectItem);
			}
		}else{
			for (User user : this.userList){
				SelectItem selectItem = new SelectItem(user.getUserId(),user.getUsername());
				selectAssignToList.add(selectItem);
			}
		}
		return selectAssignToList;
	}
	public String getAssignToId() {
		return assignToId;
	}
	public void setAssignToId(String assignToId) {
		this.assignToId = assignToId;
	}
	public String getStartVProductCode(){
		String productCode=""; 
		String voucherId=this.startVoucher.getVoucherId();
		for (Voucher voucher : this.voucherList){
			if (voucher.getVoucherId().equalsIgnoreCase(voucherId)){
				PartnerProduct product=voucher.getProduct1();
				productCode=product.getCode();
				this.startVoucher=voucher;
				break;
			}
		}
		return productCode;
	}
	public String getLastVProductCode(){
		String productCode=""; 
		String voucherId=this.lastVoucher.getVoucherId();
		for (Voucher voucher : this.voucherList){
			if (voucher.getVoucherId().equalsIgnoreCase(voucherId)){
				PartnerProduct product=voucher.getProduct1();
				productCode=product.getCode();
				this.lastVoucher=voucher;
				break;
			}
		}
		return productCode;
	}
	
	public String getAssignTo() {
		return assignTo;
	}
	public void setAssignTo(String assignTo) {
		this.assignTo = assignTo;
	}
	
	public String initVoucherAssignmentAction(){
		this.lookupForm=false;
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		this.startVoucher=new Voucher();
		this.lastVoucher=new Voucher();
		this.agent=new Agent();
		this.hgvcUser=new User();
		try{
			selectAssignTypeList = new ArrayList<SelectItem>();
			SelectItem agentSelectItem=new SelectItem(AdminConstant.AGENT,AdminConstant.AGENT);
			selectAssignTypeList.add(agentSelectItem);
			SelectItem employeeSelectItem=new SelectItem(AdminConstant.EMPLOYEE,AdminConstant.EMPLOYEE);
			selectAssignTypeList.add(employeeSelectItem);
			
			this.assignType=AdminConstant.AGENT;
			
			selectVoucherUnassignList = new ArrayList<SelectItem>();
			this.voucherList=this.voucherService.getVoucherUnassignList();
			this.agentList=this.voucherService.getAgentApprovedList();
			this.userList=this.voucherService.getUserListByRole(SystemConstant.ADMIN_ROLE);
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.MEMBER_SERVICE_ROLE));
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.SALES_ADMIN_ROLE));
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.FINANCE_ADMIN_ROLE));
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	public String voucherAssignmentAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void voucherAssignmentActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.ackTitle=SpringUtils.getMessage("voucher.assignment.ack.label", new Object[]{});
			if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT)){
				this.agent.setAgentId(this.assignToId);
				this.voucherService.voucherAssignment(this.agent, startVoucher, lastVoucher);
				for (Agent agent : this.agentList){
					if (this.assignToId.equalsIgnoreCase(agent.getAgentId()))
						this.assignTo=agent.getCode() + " - " + agent.getCompName();
				}
			}else{
				this.hgvcUser.setUserId(this.assignToId);
				this.voucherService.voucherAssignment(this.hgvcUser, startVoucher, lastVoucher);
				for(User user : this.userList){
					if (this.assignToId.equalsIgnoreCase(user.getUserId()))
						this.assignTo=user.getUsername();
				}
			}
			this.ackBindingMethod="#{voucherMgmtBean.initVoucherAssignmentAction}";
			setHtmlCmdButton();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	//---------------------Voucher Assignment End-----------------------------------------------
	public String getAckTitle() {
		return ackTitle;
	}
	
	public HtmlCommandButton getHtmlCmdButton() {
		return htmlCmdButton;
	}
	
	//--------------------Voucher Unassignment--------------------------------------------------
	public String initVoucherUnassignmentAction(){
		this.lookupForm=false;
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		this.startVoucher=new Voucher();
		this.lastVoucher=new Voucher();
		this.agent=new Agent();
		this.hgvcUser=new User();
		try{
			selectAssignTypeList = new ArrayList<SelectItem>();
			SelectItem agentSelectItem=new SelectItem(AdminConstant.AGENT,AdminConstant.AGENT);
			selectAssignTypeList.add(agentSelectItem);
			SelectItem employeeSelectItem=new SelectItem(AdminConstant.EMPLOYEE,AdminConstant.EMPLOYEE);
			selectAssignTypeList.add(employeeSelectItem);
			
			this.assignType=AdminConstant.AGENT;
			
			selectVoucherUnassignList = new ArrayList<SelectItem>();
			this.voucherList=new ArrayList<Voucher>();
			
			this.agentList=this.voucherService.getAgentApprovedList();
			this.userList=this.voucherService.getUserListByRole(SystemConstant.ADMIN_ROLE);
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.MEMBER_SERVICE_ROLE));
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.SALES_ADMIN_ROLE));
			this.userList.addAll(this.voucherService.getUserListByRole(SystemConstant.FINANCE_ADMIN_ROLE));
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public List<SelectItem> getSelectAssignedVoucherList(){
		List<SelectItem> selectList=new ArrayList<SelectItem>();
		
		try{
			if (this.assignToId!=null){
				if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT)){
					for (Agent agent : this.agentList){
						if (this.assignToId.equalsIgnoreCase(agent.getAgentId())){
							this.agent=agent;
							break;
						}
					}
				    this.voucherList=this.voucherService.getVoucherAssignedListByAgent(this.agent);
				}else{
					for(User user : this.userList){
						if (this.assignToId.equalsIgnoreCase(user.getUserId())){
							this.hgvcUser=user;
							break;
						}
					}
					this.voucherList=this.voucherService.getVoucherAssignedListByUser(this.hgvcUser);
				}	
			}
			
			for(Voucher voucher : this.voucherList){
				SelectItem selectItem=new SelectItem(voucher.getVoucherId(),voucher.getVoucherNo());
				selectList.add(selectItem);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
		return selectList;
	}
	
	public String voucherUnassignmentAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void voucherUnassignmentActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.ackTitle=SpringUtils.getMessage("voucher.unassignment.ack.label", new Object[]{});
			if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT)){
				this.agent.setAgentId(this.assignToId);
				this.voucherService.voucherUnassignment(this.agent, startVoucher, lastVoucher);
				for (Agent agent : this.agentList){
					if (this.assignToId.equalsIgnoreCase(agent.getAgentId()))
						this.assignTo=agent.getCode() + " - " + agent.getCompName();
				}
			}else{
				this.hgvcUser.setUserId(this.assignToId);
				this.voucherService.voucherUnassignment(this.hgvcUser, startVoucher, lastVoucher);
				for(User user : this.userList){
					if (this.assignToId.equalsIgnoreCase(user.getUserId()))
						this.assignTo=user.getUsername();
				}
			}
			this.ackBindingMethod="#{voucherMgmtBean.initVoucherUnassignmentAction}";
			setHtmlCmdButton();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	//--------------------Voucher Unassignment End----------------------------------------------
	public String getAckBindingMethod() {
		return ackBindingMethod;
	}
	private void setHtmlCmdButton() {
		Application app = FacesContext.getCurrentInstance().getApplication();
		HtmlCommandButton htmlCmdButton = (HtmlCommandButton)app.createComponent(HtmlCommandButton.COMPONENT_TYPE);
		
		htmlCmdButton.setValue(SpringUtils.getMessage("back", new Object[]{}));
		htmlCmdButton.setStyleClass("button");
		htmlCmdButton.setImmediate(true);
		MethodBinding mb = app.createMethodBinding(ackBindingMethod,null);
		htmlCmdButton.setAction(mb);
		
		this.htmlCmdButton = htmlCmdButton;
	}
	
	//------------Voucher Listing Report-------------------------
	
	public String getVoucherNo() {
		return voucherNo;
	}
	public void setVoucherNo(String voucherNo) {
		this.voucherNo = voucherNo;
	}
	public Date getEffectiveDate() {
		return effectiveDate;
	}
	public void setEffectiveDate(Date effectiveDate) {
		this.effectiveDate = effectiveDate;
	}
	public Date getExpiryDate() {
		return expiryDate;
	}
	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}
	public String initVoucherListRptParamAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		return outcome;
	}
	
	public void generateVoucherListReportAction(){
		List<VoucherBean> list=new ArrayList<VoucherBean>();
		try {
			list = this.voucherService.getVoucherReportList(this.voucherNo,this.effectiveDate,this.expiryDate);
			
			ReportBean reportBean=(ReportBean) getNewManagedBean(ReportBean.BEAN_NAME);
			reportBean.createReport(new HashMap(), list, "voucher_listing");
		} catch (BaseException be) {
			initErrorBean(be);
		}
	}
	//------------Voucher Listing Report End---------------------
	//------------Voucher Entry Start----------------------------
	public Agreement getAgreement() {
		return agreement;
	}
	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}
	public Guest getGuest() {
		return guest;
	}
	public void setGuest(Guest guest) {
		this.guest = guest;
	}
	public VoucherEntry getVoucherEntry() {
		return voucherEntry;
	}
	public void setVoucherEntry(VoucherEntry voucherEntry) {
		this.voucherEntry = voucherEntry;
	}
	public String getVoucherEntryType() {
		return voucherEntryType;
	}
	public void setVoucherEntryType(String voucherEntryType) {
		this.voucherEntryType = voucherEntryType;
	}
	public List<SelectItem> getSelectMemberList() {
		return selectMemberList;
	}
	public List<SelectItem> getSelectAgreementList() {
		return selectAgreementList;
	}
	public List<SelectItem> getSelectVoucherEntryTypeList() {
		return selectVoucherEntryTypeList;
	}
	public List<VoucherEntryDetail> getVoucherEntryDetailList() {
		return voucherEntryDetailList;
	}
	public Prospect getProspect() {
		return prospect;
	}
	public void setProspect(Prospect prospect) {
		this.prospect = prospect;
	}
	public VoucherEntryDetail getVoucherEntryDetail() {
		return voucherEntryDetail;
	}
	public void setVoucherEntryDetail(VoucherEntryDetail voucherEntryDetail) {
		this.voucherEntryDetail = voucherEntryDetail;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public List<SelectItem> getSelectVoucherList() {
		return selectVoucherList;
	}
	
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	
	public List<SelectItem> getSelectProspectList() {
		return selectProspectList;
	}
	
	public String initVoucherEntryAction(){
		this.lookupForm=false;
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		try{
			selectVoucherEntryTypeList=new ArrayList<SelectItem>();
			SelectItem memberSelectItem=new SelectItem(AdminConstant.MEMBER,AdminConstant.MEMBER);
			selectVoucherEntryTypeList.add(memberSelectItem);
			SelectItem nonMemberSelectItem=new SelectItem(AdminConstant.NON_MEMBER,AdminConstant.NON_MEMBER);
			selectVoucherEntryTypeList.add(nonMemberSelectItem);
			SelectItem guestSelectItem=new SelectItem(AdminConstant.GUEST,AdminConstant.GUEST);
			selectVoucherEntryTypeList.add(guestSelectItem);
			
			this.voucherEntryType=AdminConstant.MEMBER;
			
			this.memberList=this.memberService.getMemberList();
			
			
			selectMemberList=new ArrayList<SelectItem>();
			for (Member m : this.memberList){
				String memberName = this.memberService.getMembershipName(m);
				String membershipNo = m.getMembershipNo();
				if (membershipNo==null) membershipNo="N/A";
				SelectItem selectItem = new SelectItem(m.getMemberId(),membershipNo + " - "+memberName);
				selectMemberList.add(selectItem);
			}
			
			selectAgreementList=new ArrayList<SelectItem>();
			
			this.selectProspectList=new ArrayList<SelectItem>();
			
			List<Member> propectList=this.memberService.getProspectMemberList();
			for(Member mp : propectList){
				String memberName = this.memberService.getMembershipName(mp);
				SelectItem selectItem=new SelectItem(mp.getMemberId(),mp.getMemberRegNo()+" - "+memberName);
				this.selectProspectList.add(selectItem);
			}
			
			this.member=new Member();
			this.voucherEntry=new VoucherEntry();
			this.prospect=new Prospect();
			this.guest=new Guest();
			this.agreement=new Agreement();
			this.voucher=new Voucher();
			this.voucherEntryDetailList=new ArrayList<VoucherEntryDetail>();
			this.voucherEntryDetail=new VoucherEntryDetail();
			refreshSelectVoucherList();
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	private void refreshSelectVoucherList()throws BaseException{
		this.voucherList=this.voucherService.getVoucherAvailableListByUser(getThisUser());
		this.selectVoucherList=new ArrayList<SelectItem>();
		for (Voucher voucher : this.voucherList){
			SelectItem selectItem=new SelectItem(voucher.getVoucherId(),voucher.getVoucherNo());
			this.selectVoucherList.add(selectItem);
		}
	}
	
	public String refreshVoucherEntryAction(){
		//System.out.println("refreshVoucherEntryAction()");
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	
	public void refreshVoucherEntryActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			/*if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.MEMBER))
				this.voucherEntry=this.voucherService.getVoucherEntryByAgreement(agreement);
			else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.NON_MEMBER))
				this.voucherEntry=this.voucherService.getVoucherEntryByProspect(this.member);
			else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.GUEST)){
				Guest guestL=this.voucherService.getGuestByIcNo(this.guest.getIcNo());
				if (guestL!=null)
					this.guest = guestL;
				this.voucherEntry=this.voucherService.getVoucherEntryByGuest(guestL);
			}
		
			voucherEntryDetailList=new ArrayList<VoucherEntryDetail>();
			voucherEntryDetailList=this.voucherService.getVoucherEntryDetailList(voucherEntry==null?new VoucherEntry():this.voucherEntry);*/
			refreshVoucherEntryUpdate();
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public void refreshVoucherEntryUpdate() throws BaseException{
		VoucherEntry voucherEntryL = null;
		//System.out.println("refreshVoucherEntryUpdate()...");
		if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.MEMBER))
			voucherEntryL=this.voucherService.getVoucherEntryByAgreement(agreement);
		else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.NON_MEMBER))
			voucherEntryL=this.voucherService.getVoucherEntryByProspect(this.member);
		else if (this.voucherEntryType.equalsIgnoreCase(AdminConstant.GUEST)){
			Guest guestL=this.voucherService.getGuestByIcNo(this.guest.getIcNo());
			if (guestL!=null)
				this.guest = guestL;
			voucherEntryL=this.voucherService.getVoucherEntryByGuest(guestL);
		}
		
		voucherEntryDetailList=new ArrayList<VoucherEntryDetail>();
		if (voucherEntryL!=null){
			this.voucherEntry=voucherEntryL;
		    voucherEntryDetailList=this.voucherService.getVoucherEntryDetailList(voucherEntryL);
		}
	}
	
	public String navAddVoucherEntryAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		
		VoucherEntryBean bean = (VoucherEntryBean) getNewManagedBean(VoucherEntryBean.BEAN_NAME);
		bean.setAgreement(this.agreement);
		bean.setGuest(this.guest);
		bean.setMember(this.member);
		bean.setVoucherEntry(this.voucherEntry);
		bean.setVoucherEntryType(this.voucherEntryType);
		bean.setVoucherEntryDetail(this.voucherEntryDetail);
		bean.setVoucher(this.voucher);
		try {
			refreshSelectVoucherList();
		} catch (BaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void setVoucherEntryDetailList(
			List<VoucherEntryDetail> voucherEntryDetailList) {
		this.voucherEntryDetailList = voucherEntryDetailList;
	}
	
	public String deleteVoucherEntryAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void deleteVoucherEntryActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<VoucherEntryDetail> deleteList=new ArrayList<VoucherEntryDetail>();
			for (VoucherEntryDetail voucherEntryDetail : this.voucherEntryDetailList){
				if (voucherEntryDetail.isMarkAsSelect())
					deleteList.add(voucherEntryDetail);
			}
			this.voucherService.removeVoucherEntry(deleteList);
			
			refreshVoucherEntryUpdate();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	//------------Voucher Entry End-------------------------------
	
	public String initPrintComplAccommVoucherAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		try{
			this.voucherList=this.voucherService.getVoucherListSellByUser(getThisUser());
			this.selectVoucherList=new ArrayList<SelectItem>();
			for (Voucher voucher : this.voucherList){
				SelectItem selectItem=new SelectItem(voucher.getVoucherId(),voucher.getVoucherNo());
				this.selectVoucherList.add(selectItem);
			}
			this.voucher=new Voucher();
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public void generateVoucherComplimentaryAccomodationAction(){
		List<VoucherBean> list=new ArrayList<VoucherBean>();
		try {
			list = this.voucherService.getComplimentaryAccommVoucherList(this.voucher);
			
			ReportBean reportBean=(ReportBean) getNewManagedBean(ReportBean.BEAN_NAME);
			reportBean.createReport(new HashMap(), list, "compl_acco_voucher");
		} catch (BaseException be) {
			initErrorBean(be);
		}
	}
	
	public String backVoucherAssignmentAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		return outcome;
	}
	public String backVoucherUnAssignmentAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		return outcome;
	}
	public void lookupFormStartVoucherAssignActionEvent(ActionEvent event){
		this.lookupForm=true;
		this.startVoucherLookup=true;
		this.lastVoucherLookup=false;
	}
	public void lookupFormLastVoucherAssignActionEvent(ActionEvent event){
		this.lookupForm=true;
		this.lastVoucherLookup=true;
		this.startVoucherLookup=false;
	}
	public void lookupFormVoucherAssignSelectedActionEvent(ActionEvent event){
		this.lookupForm=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("voucherId");
		String voucherId=param.getValue().toString();
		for(Voucher voucherP : this.voucherList){
			if (voucherP.getVoucherId().equalsIgnoreCase(voucherId)){
				if (this.startVoucherLookup){
					startVoucher=voucherP;
					startVoucher.setVoucherId(voucherId);
				}else{
					lastVoucher=voucherP;
					lastVoucher.setVoucherId(voucherId);
				}
				
				break;
			}
		}
	}
	public void lookupFormVoucherUnassignSelectedActionEvent(ActionEvent event){
		this.lookupForm=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("voucherId");
		String voucherId=param.getValue().toString();
		for(Voucher voucherP : this.voucherAssignedList){
			if (voucherP.getVoucherId().equalsIgnoreCase(voucherId)){
				if (this.startVoucherLookup){
					startVoucher=voucherP;
					startVoucher.setVoucherId(voucherId);
				}else{
					lastVoucher=voucherP;
					lastVoucher.setVoucherId(voucherId);
				}
				
				break;
			}
		}
	}
	public void lookupFormVoucherAssignCloseActionEvent(ActionEvent event){
		this.lookupForm=false;
	}
	public boolean isStartVoucherLookup() {
		return startVoucherLookup;
	}
	public boolean isLastVoucherLookup() {
		return lastVoucherLookup;
	}
	public List<Voucher> getVoucherList() {
		return voucherList;
	}
	
	public List<Voucher> getVoucherAssignedList() {
		this.voucherAssignedList=new ArrayList<Voucher>();
		try{
			if (this.assignToId!=null){
				if (this.assignType.equalsIgnoreCase(AdminConstant.AGENT)){
					for (Agent agent : this.agentList){
						if (this.assignToId.equalsIgnoreCase(agent.getAgentId())){
							this.agent=agent;
							break;
						}
					}
				    this.voucherAssignedList=this.voucherService.getVoucherAssignedListByAgent(this.agent);
				}else{
					for(User user : this.userList){
						if (this.assignToId.equalsIgnoreCase(user.getUserId())){
							this.hgvcUser=user;
							break;
						}
					}
					this.voucherAssignedList=this.voucherService.getVoucherAssignedListByUser(this.hgvcUser);
				}	
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
		return voucherAssignedList;
	}
	public boolean isLookupForm() {
		return lookupForm;
	}
	
	public void refreshSelectAgreementsListener(ActionEvent event){
		try{
			List<Agreement> list = this.memberService.getAgreementListByMemberId(this.member.getMemberId());
			
			this.selectAgreementList= new ArrayList<SelectItem>();
			
			for (Agreement a : list){
				this.selectAgreementList.add(new SelectItem(a.getAgreementId(),a.getAgreetmentNo()));
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
}
